<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package}.${mapper}">

    <#if save>
    <insert id="save" parameterType="${class}" useGeneratedKeys="true" keyColumn="${id_column}" keyProperty="${id_property}">
        insert into ${table_name} (${save_columns}) values (${save_properties})
    </insert>
    </#if>
    
    <#if update>
    <update id="update" parameterType="${class}">
        update ${table_name} <set> ${update_fields} </set> where ${by_id}
    </update>
    </#if>
    <#if deleteBy>
    <#list deleteby_query as q>

    <delete id="${q.id}" parameterType="${q.xmltype}">
        delete from ${table_name} where ${q.by}
    </delete>
    </#list>
    </#if>
    <#if findBy>
    <#list findby_query as q>
    
    <select id="${q.id}" resultMap="findMap" parameterType="${q.xmltype}">
        select ${select_columns} from ${table_name} where ${q.by}
    </select>
    </#list>
    </#if>
    
    <#if findAll>
    <select id="findAll" resultMap="findMap">
        select ${select_columns} from ${table_name}
    </select>
    </#if>
    
    <#if findCount>
    <select id="findCount" resultType="int">
        select count(*) from ${table_name}
    </select>
    </#if>
    
    <#if findByVo>
    <#if dialect=="oracle">
    <select id="findByVo" resultMap="findMap" parameterType="${voclass}">
        <![CDATA[ 
        select ${select_columns} from 
        (
            select ${select_columns},rownum rn from
            (
                select ${select_columns} from ${table_name}
        ]]>
                 ${conditions} <if test="sort!=null and order!=null">order by ${r"${sort}"} ${r"${order}"}"</if>
        <![CDATA[
            ) a
           where rownum <= ${r"#{end}"}
        )
        where rn > ${r"#{begin}"}
        ]]>
    </select>
    <#elseif dialect=="mysql">
    <select id="findByVo" resultMap="findMap" parameterType="${voclass}">
        select ${select_columns} from ${table_name} ${conditions}<if test="sort!=null and order!=null"> order by ${r"${sort}"} ${r"${order}"}</if> limit ${r"#{offset}"}, ${r"#{limit}"}
    </select>
    </#if>
    
    <select id="findCountByVo" resultType="int">
        select count(*) from ${table_name} ${conditions}
    </select>
    </#if>
    
    <resultMap id="findMap" type="${class}">
        ${result_map}
    </resultMap>

</mapper>